

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import devboard.dbutil.MongoUtil;
public class RegisterHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
    public RegisterHandler() {
        super();
       
    }
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBCollection col=MongoUtil.getCollection("Users");
		BasicDBObject obj=new BasicDBObject("_id",request.getParameter("email"));
		if(col.findOne(obj)!=null){
			response.sendError(500);
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBCollection col=MongoUtil.getCollection("Users");
		BasicDBObject doc = new BasicDBObject();
		doc.put("company", request.getParameter("company"));
		doc.put("firstName", request.getParameter("firstName"));
		doc.put("lastName", request.getParameter("lastName"));
		doc.put("_id", request.getParameter("email"));
		doc.put("password", request.getParameter("userPass"));
		doc.put("role", "nobody");
		col.insert(doc);
		
		DBCollection properties=MongoUtil.getCollection("Properties");
		DBObject property=properties.findOne(new BasicDBObject("_id","devboard"));
		property.put("_id", request.getParameter("email"));
		properties.insert(property);
		response.sendRedirect("Login.jsp");				
	}

}
